package day08.test3;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

class Tree {
    private int num;

    public Tree() {
        this.num = 100;
    }

    public Tree(int num) {
        this.num = num;
    }

    public int get(){
        return this.num;
    }

    public void getNum() {
        System.out.println("仙女说现在现在还有" + this.num + "个桃子");
    }

    //增加桃子
    public void add() {
        Random res = new Random();
        int Randomnum = res.nextInt(10);
        this.num += Randomnum;
        System.out.println("树增加了" + Randomnum + "个桃子，现在还有" + this.num + "个桃子");
    }

    //猴子吃桃子
    public void monkey() {
        if (this.num > 0) {
            this.num--;
            System.out.println("猴子吃了1个桃子，还剩"+this.num+"个桃子");
        } else {
            System.out.println("没有桃子了");
        }
    }
}

class Monkey implements Runnable {
    private Tree tree;

    public Monkey(Tree tree) {
        this.tree = tree;
    }

    @Override
    public void run() {
        while (true) {
            tree.monkey();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Tree_num_add implements Runnable {
    private Tree tree;

    public Tree_num_add(Tree tree) {
        this.tree = tree;
    }

    @Override
    public void run() {
        while (true) {
            tree.add();
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class woman implements Runnable {
    private Tree tree;
    private CountDownLatch countDownLatch;

    public woman(Tree tree, CountDownLatch countDownLatch) {
        this.tree = tree;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        try {
            countDownLatch.countDown();
        } catch (Exception e) {
            e.printStackTrace();
        }
        while (true) {
            tree.getNum();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


public class test {
    public static void main(String[] args) {
        Tree tree = new Tree();
        System.out.println(tree.get());

        CountDownLatch countDownLatch=new CountDownLatch(1);

        Thread thread1 = new Thread(new Monkey(tree));
        Thread thread2 = new Thread(new Tree_num_add(tree));
        Thread thread3 = new Thread(new woman(tree,countDownLatch));
        thread3.start();

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread1.start();
        thread2.start();
    }
}
/**
 * 在Monkey线程内部，num变量的递减操作和打印操作可能在物理执行顺序上并不一定是严格按照代码顺序来的。
 * 例如，Monkey线程可能在执行this.num--操作后，被暂停，此时num已经变成了99。
 * 然后woman线程被调度执行，输出仙女说现在现在还有99个桃子。
 * 之后Monkey线程再继续执行，输出猴子吃了1个桃子，还剩99个桃子。
 */
